package com.tailyou.hms.firstapp.act;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.adapter.ListAdapter;
import com.tailyou.hms.firstapp.adapter.MenuAdapter;
import com.tailyou.hms.firstapp.bean.MLBean;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MenuListActivity extends AppCompatActivity {

    private RecyclerView rvMenu, rvList;
    private MenuAdapter adapterMenu;
    private ListAdapter adapterList;
    private List<MLBean.DataDTO.ListDTO> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_list);
        rvMenu = findViewById(R.id.rvMenu);
        rvList = findViewById(R.id.rvList);
        doGet("https://www.fastmock.site/mock/aaaf835a0d6fd5c09c5c127ed1129afb/demo01/api/house/catgory");
    }

    /**
     * GET请求
     *
     * @param url
     */
    private void doGet(String url) {
        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(url).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String repContent = response.body().string();
                        Gson gson = new Gson();
                        MLBean mlBean = gson.fromJson(repContent, MLBean.class);
                        runOnUiThread(() -> {
                            // MENU
                            adapterMenu = new MenuAdapter(mlBean.getData()) {
                                @Override
                                public void clickItem(int position) {
                                    datas = mlBean.getData().get(position).getList();
                                    adapterList.updateDatas(datas);
                                }
                            };
                            rvMenu.setLayoutManager(new LinearLayoutManager(MenuListActivity.this));
                            rvMenu.setAdapter(adapterMenu);
                            // LIST
                            datas = mlBean.getData().get(0).getList();
                            adapterList = new ListAdapter(datas) {
                                @Override
                                public void clickItem(int position) {

                                }
                            };
                            rvList.setLayoutManager(new LinearLayoutManager(MenuListActivity.this));
                            rvList.setAdapter(adapterList);
                        });
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}